package com.sneakxy.cloudbase.platform.service;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sneakxy.cloudbase.platform.bean.Role;
import com.sneakxy.cloudbase.platform.repository.RoleRepository;
import com.sneakxy.mybatis.commons.repository.query.domain.CriteriaBuilder;

/**
 * 
 * @author 潜行的青衣
 * @see
 */
@Service
public class RoleService extends BaseService<Role, String, RoleRepository> implements IRoleService {

	@Autowired
	private IRolePermService rolePermService;
	
	@Autowired
	private IAccountRoleService accountRoleService;
	
	/* (non-Javadoc)
	 * @see com.sneakxy.cloudbase.platform.service.IRoleService#findBySearch(java.lang.String, org.springframework.data.domain.Pageable)
	 */
	@Override
	public Page<Role> findBySearch(String search, Pageable pageable) {
		if(StringUtils.isBlank(search)) {
			return this.getRepository().findAll(pageable);
		}
		String value = StringUtils.join("%", StringUtils.trimToEmpty(search), "%");
		CriteriaBuilder<Role> builder = new CriteriaBuilder<>(Role.class);
		builder.orLike("id", value);
		builder.orLike("name", value);
		builder.orLike("memo", value);
		return this.getRepository().findAll(builder.build(), pageable);
	}

	@Override
	@Transactional
	public void removeCascad(String id) {
		rolePermService.deleteByRoleId(id);
		accountRoleService.deleteByRoleId(id);
		delete(id);
	}

}
